import {
    validate,
    Length
} from 'class-validator'

import { isError } from 'lodash'

import { Wallet } from '@app/wallet'
import { nothrow } from '@pkg/promise'
import * as utils from '@app/methods/utils'

class Request {
    @Length(64)
    txid: string = ''
}

/**
 * 获取交易信息
 */
export async function getTransaction(wallet: Wallet, params: any, callback: any) {
    const model = utils.makeModel(new Request, params)
    const errors = await validate(model)
    if (errors.length > 0) {
        callback(utils.BadRequest2(errors), undefined)
        return
    }

    const info = await nothrow(wallet.tronWeb.trx.getTransactionInfo(model.txid))
    if (isError(info)) {
        callback({code: -32603, message: info.message}, undefined)
        return
    }
    callback(undefined, info)
}
